import csv
from threading import Lock

class CSVFileManager:
    def __init__(self, csv_path, quoting=csv.QUOTE_MINIMAL):
        open(csv_path, mode='a').close()
        self.csv_path = csv_path
        self.quoting = quoting
        self.lock = Lock()

    def clear(self):
        open(self.csv_path, mode='w').close()

    def read_csv(self):
        with open(self.csv_path, mode='r', newline='', encoding='utf-8') as file:
            reader = csv.reader(file, quoting=self.quoting)
            return list(reader)

    def write_csv(self, data):
        with open(self.csv_path, mode='w', newline='', encoding='utf-8') as file:
            writer = csv.writer(file, quoting=self.quoting)
            writer.writerows(data)

    def insert_first_row(self, row):
        with self.lock:
            data = self.read_csv()
            data.insert(0, row)
            self.write_csv(data)

    def insert_row_at(self, row, index):
        with self.lock:
            data = self.read_csv()
            data.insert(index, row)
            self.write_csv(data)

    def append_row(self, row):
        with self.lock:
            with open(self.csv_path, mode='a', newline='', encoding='utf-8') as file:
                writer = csv.writer(file, quoting=self.quoting)
                writer.writerow(row)

    def append_rows(self, rows):
        with self.lock:
            with open(self.csv_path, mode='a', newline='', encoding='utf-8') as file:
                writer = csv.writer(file, quoting=self.quoting)
                writer.writerows(rows)
